package com.baicaoyuan.web.repository;

import com.baicaoyuan.web.model.ProjectItemUserDO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.math.BigInteger;
import java.util.List;

@Repository
public interface ProjectItemUserRepository extends JpaRepository<ProjectItemUserDO, Long> {

    @Transactional
    @Modifying
    @Query(value = "update t_project_item_user set status = ?2, reject_reason =?3 where id = ?1", nativeQuery = true)
    Integer updateStatus(Long id, String status, String rejectReason);

    List<ProjectItemUserDO> findByProjectItemIdAndStatusIsNotOrderByCreateTimeDesc(Long projectItemId, String status);

    List<ProjectItemUserDO> findByUserIdOrderByCreateTimeDesc(Long userId);

    List<ProjectItemUserDO> findByStatusInOrderByCreateTimeDesc(List<String> status, Pageable pageable);


    @Transactional
    @Modifying
    @Query(value = "SELECT project_item_id FROM t_project_item_user where project_id = ?1 and `status` = ?3 and user_id = ?2", nativeQuery = true)
    List<BigInteger> findByProjectIdAndUserIdAndStatus(Long projectId, Long userId, String status);

    List<ProjectItemUserDO> findByUserIdAndStatusNot(Long userId,String status);
}
